<!DOCTYPE html>
<html lang="en" xmlns:sec="http://www.thymeleaf.org/extras/spring-security" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('定时任务列表')"/>
</head>
<body>
    <div class="layuimini-container">
        <div class="layuimini-main">

            <fieldset class="table-search-fieldset">
                <legend>搜索信息</legend>
                <div style="margin: 10px 10px 10px 10px">
                    <form action="" class=" layui-form layui-form-pane">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">任务名称</label>
                                <div class="layui-input-inline">
                                    <input class="layui-input" name="jobName" placeholder="" type="text">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <button class="layui-btn layui-btn-md" lay-filter="job-query" lay-submit>
                                    <i class="layui-icon layui-icon-search"></i>
                                    查询
                                </button>
                                <button class="layui-btn layui-btn-md layui-btn-primary" type="reset">
                                    <i class="layui-icon layui-icon-refresh"></i>
                                    重置
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </fieldset>
            <table id="job-table" lay-filter="job-table"></table>
        </div>
    </div>
</body>
<script id="job-toolbar" type="text/html">
    <button sec:authorize="hasPermission('/system/job/add','sys:job:add')"
            class="layui-btn layui-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
    <button class="layui-btn layui-btn-primary layui-btn-md" lay-event="runLog">
        <i class="layui-icon layui-icon-file"></i>
        日志
    </button>
</script>
<script id="job-bar" type="text/html">
    {{#if (d.status == 0) { }}
    <button sec:authorize="hasPermission('/system/job/pause','sys:job:pause')"
            class="layui-btn layui-btn-warming layui-btn-sm" lay-event="pause"><i class="layui-icon layui-icon-pause"></i>
    </button>
    {{# }else if(d.status == 1){ }}
    <button sec:authorize="hasPermission('/system/job/resume','sys:job:resume')"
            class="layui-btn layui-btn-warming layui-btn-sm" lay-event="resume"><i class="layui-icon layui-icon-play"></i>
    </button>
    {{# } }}
    <button sec:authorize="hasPermission('/system/job/run','sys:job:run')"
            class="layui-btn layui-btn-success layui-btn-sm" lay-event="run"><i class="layui-icon layui-icon-release"></i>
    </button>
    <button sec:authorize="hasPermission('/system/job/edit','sys:job:edit')"
            class="layui-btn layui-btn-primary layui-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>
    </button>
    <button sec:authorize="hasPermission('/system/job/remove','sys:job:del')"
            class="layui-btn layui-btn-danger layui-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i>
    </button>
</script>
<script id="job-status" type="text/html">
    {{#if (d.status == 0) { }}
    <span>运行中</span>
    {{# }else if(d.status == 1){ }}
    <span>已挂起</span>
    {{# } }}
</script>
<th:block th:include="include :: footer"/>
<script>
    var table;
    layui.use(function () {
        table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;

        let prefix = pName + "/system/job/";

        let cols = [
            [
                {type: 'checkbox'},
                {title: '名称', field: 'jobName', align: 'center', width: 100},
                {title: '运行类', field: 'beanName', align: 'center'},
                {title: '参数', field: 'params', align: 'center'},
                {title: '表达式', field: 'cronExpression', align: 'center'},
                {title: '状态', field: 'status', align: 'center', templet: '#job-status'},
                {title: '描述', field: 'remark', align: 'center'},
                {title: '操作', toolbar: '#job-bar', align: 'center', width: 250}
            ]
        ]

        table.render({
            elem: '#job-table',
            url: prefix + 'data',
            page: true,
            escape: true,
            cols: cols,
            skin: 'line',
            toolbar: '#job-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
            ,height: 'full-135'
        });

        table.on('checkbox(job-table)', function(obj){

        });
        table.on('row(job-table)', function (obj) {

        });
        table.on('tool(job-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            } else if (obj.event === 'pause') {
                window.pause(obj);
            } else if (obj.event === 'resume') {
                window.resume(obj);
            } else if (obj.event === 'run') {
                window.run(obj);
            }
        });

        table.on('toolbar(job-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'runLog') {
                window.runLog();
            }
        });

        form.on('submit(job-query)', function (data) {
            table.reload('job-table', {where: data.field})
            return false;
        });

        window.add = function () {
            let layerIndex = layer.open({
                type: 2,
                title: '新增',
                shade: 0.1,
                area: ['99%', '99%'],
                content: prefix + 'add'
            });
            $(window).on("resize", function () {
                layer.full(layerIndex);
            });
            return false;
        }

        window.edit = function (obj) {
            let layerIndex = layer.open({
                type: 2,
                title: '修改',
                shade: 0.1,
                area: ['99%', '99%'],
                content: prefix + 'edit?jobId=' + obj.data['jobId']
            });
            $(window).on("resize", function () {
                layer.full(layerIndex);
            });
            return false;
        }

        window.runLog = function () {
            let layerIndex = layer.open({
                type: 2,
                title: '运行日志',
                shade: 0.1,
                area: ['99%', '99%'],
                content: pName + '/system/job/log/main'
            });
            $(window).on("resize", function () {
                layer.full(layerIndex);
            });
            return false;
        }

        window.pause = function (obj) {
            let loading = layer.load();
            $.ajax({
                url: pName + '/system/job/pause?jobId=' + obj.data['jobId'],
                dataType: 'json',
                contentType: 'application/json',
                type: 'put',
                success: function (result) {
                    layer.close(loading);
                    if (result.code == 1) {
                        layer.msg(result.msg, {time: 500}, function () {
                            table.reload("job-table");
                        });
                    } else {
                        layer.alert(result.msg);
                    }
                }
            })
        }

        window.resume = function (obj) {
            let loading = layer.load();
            $.ajax({
                url: pName + '/system/job/resume?jobId=' + obj.data['jobId'],
                dataType: 'json',
                contentType: 'application/json',
                type: 'put',
                success: function (result) {
                    layer.close(loading);
                    if (result.code == 1) {
                        layer.msg(result.msg, {time: 500}, function () {
                            table.reload("job-table");
                        });
                    } else {
                        layer.alert(result.msg);
                    }
                }
            })
        }

        window.run = function (obj) {
            let loading = layer.load();
            $.ajax({
                url: pName + '/system/job/run?jobId=' + obj.data['jobId'],
                dataType: 'json',
                contentType: 'application/json',
                type: 'put',
                success: function (result) {
                    layer.close(loading);
                    if (result.code == 1) {
                        layer.msg(result.msg);
                    } else {
                        layer.alert(result.msg);
                    }
                }
            })
        }

        window.remove = function (obj) {
            layer.confirm('确定要删除该任务', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: prefix + "remove/" + obj.data['jobId'],
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 1) {
                            layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                obj.del();
                            });
                        } else {
                            layer.alert(result.msg, {icon: 2});
                        }
                    }
                })
            });
        }


        window.refresh = function (param) {
            table.reload('job-table');
        }

    })
</script>
</html>